<script type="text/javascript" src="<?php echo Yii::app()->params['urlRs']; ?>/js/tiny_mce/tiny_mce.js"></script> 
<?php $url = new Url();?> 
<div class="left">
    <?php $this->widget("AdminMessenger")?>   
</div>
<div class="right-main">
    <div class="righ-cont">
        <div class="right">
            <div class="title_style"> <span><b>Soạn thư</b></span></div>
            <div class="tool clearfix">
                <ul class="form1">
                    <li>
                        <label><strong>Gửi cho tất cả:</strong></label>
                        <div>
                            <p><input type="checkbox" id="everyone" onclick="disable();"/> </p>
                        </div>
                    </li>
                    <li class="clearfix" id="to">
                        <label><strong>To:</strong></label>
                        <div class="filltext">                           
                            <p>
                                <input type="text" id="user_name" style="width:220px" onchange="checkUser(this.value);" />
                                <div id="report"></div>
                            </p>
                        </div>
                    </li>
                    <li class="clearfix">
                        <label><strong>Tiêu đề:</strong> </label>
                        <div class="filltext">
                            <p>
                                <input type="text" id="title" style="width:220px"/>
                            </p>
                        </div>
                    </li>
                    <li class="clearfix">
                        <label><strong>Nội dung:</strong> </label>
                        <div class="filltext">
                            <div class="box">
                                <textarea name="content1" id="content1" style=" width:98%" rows="30">
                                    <?=isset($data['customer'])?$data['customer']:''?>
                                </textarea>
                            </div>
                        </div>
                    </li>
                    <li class="clearfix">
                        <label>&nbsp; </label>
                        <div class="filltext">
                            <input type="button" class="button" value="Gửi" onclick="insertMessenger()" />
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
<script>
var user_id = 0;
    tinyMCE.init({
        // General options
        mode : "exact",
        elements  : "content1",
        theme : "advanced",
        theme_advanced_path : true,
        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
        height : "313",
        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,cleanup,help,code",  
        theme_advanced_buttons2 : "undo,redo,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",
    });
    
    function disable(){

        if($('#everyone').attr('checked')){
            $('#to').slideUp();
        }else{
            $('#to').slideDown();    
        }
    }
        
    function checkUser(user_name){

        var strUrl ="<?=$url->createUrl('messenger/checkuser')?>";
        $.ajax({
            type:"POST",
            url:strUrl,
            data:{user_name:user_name},
            success:function(msg){  
                if(msg == 0){  
                    $('#report').html("<a style='color: red;'>User không tồn tại!</a>");
                    return false;
                }else{
                    $('#report').html("<a style='color: blue;'>User hợp lệ!</a>");
                    user_id = msg;
                }
            }
        });           
    }
    
    function insertMessenger(){
        tinyMCE.triggerSave(); 
        if($('#everyone').attr('checked')){
           var user_name = "Everyone";
        }else{
            var user_name = $('#user_name').val();     
        }

        var title = $('#title').val(); 
        var content = $('#content1').val();  
        var strUrl ="<?=$url->createUrl('messenger/insertmessenger')?>";
        $.ajax({
            type:"POST",
            url:strUrl,
            data:{  
                user_name:user_name,
                user_id:user_id,
                title:title,
                content:content
            },
            success:function(msg){    
                if(msg != 0){
                    alert("Thư đã được gửi!");
                    window.location = '<?="http://".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI']?>';
                }else{
                    alert("Gửi thư thất bại!"); 
                }
            }
        });     
    } 
</script>
